#include <multiboot.h>
#include <common.h>

void *memset(void *s, int c, int n)
{
	unsigned char* p=s;
	while(n--)
	{
		*p++ = (unsigned char)c;
	}
	return s;
}

unsigned char inportb (unsigned short _port)
{
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

void outportb (unsigned short _port, unsigned char _data)
{
    __asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}

int kmain(void)
{
	extern unsigned int magic;
	//extern struct multiboot_info *mbd;
	clear_screen();
	if (magic != 0x2BADB002) {print("WRONG MAGIC");}
	print("Mapping memory from Multiboot memory map....................................");

	/*if ((mbd->flags & (1 << 0)) && (mbd->flags & (1 << 6))) //If bits 0 and 6 are set
	{
		unsigned int i;
		int* ptr = (int*)mbd->mmap_addr;
		for (i = 0; i < mbd->mmap_length; i++)
		{
			print("-");
			print(itoa(*ptr+i, 10));
		}
	}*/
	print_OK();
	print("Starting up Global Descriptor Table.........................................");
	init_gdt();
	print("Starting up interrupts......................................................");
	init_idt();
	init_isrs();
	init_irq();
	__asm__ __volatile__ ("sti");
	print_OK();
	init_mman();
	print("Installing PIT and Keyboard driver..........................................");
	init_timer();
	init_keyboard();
	print("Done setting up basic neccesities. More to come...\n");
	
	for(;;);
}